package net.huashitong.common.controller;


import com.alibaba.fastjson.JSON;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.common.entity.SysMessage;
import net.huashitong.common.dto.MsgParams;
import net.huashitong.common.filter.SysMessageFilter;
import net.huashitong.common.service.SysMessageService;
import net.huashitong.common.util.PushMsgUtil;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 消息管理
 * @author
 */
@Controller
@RequestMapping(value = "/common/sms")
public class SysMessageController extends JsonBaseController {
    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private PushMsgUtil pushMsgUtil;
    @Override
    protected String getView(String s) {
        return "/modules/common/sms/"+s;
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid iniPageData(SysMessageFilter filter) {
        List<SysMessage> messageList = sysMessageService.queryByFilter(filter);
        return coverJqGrid(filter,messageList);
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage(Model model) {
        SysUser user = sysUserService.getById(SysUserUtils.getUserId());
        model.addAttribute("user", user);
        return getView("query");
    }

    @RequestMapping("/filter")
    public String filter(@ModelAttribute SysMessageFilter filter) {
        return getView("filter");
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        SysMessage sysMessage = sysMessageService.getById(id);
        model.addAttribute("SysMessageFilter",sysMessage);
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute SysMessageFilter filter) {
        if (StringUtils.isBlank(filter.getSysId())) {
            return coverMessage(UtilsTools.ERROR,"消息不存在");
        }
        SysMessage sysMessage = new SysMessage();
        UtilsTools.copyPro(filter,sysMessage);
        sysMessageService.update(sysMessage);
        return coverMessage(UtilsTools.CODE);
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute SysMessageFilter filter , Model model) {
        model.addAttribute("SysMessageFilter",filter);
        return getView("form");
    }
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute SysMessageFilter filter) {
        SysMessage sysMessage = new SysMessage();
        UtilsTools.copyPro(filter,sysMessage);
        sysMessageService.save(sysMessage);
        return coverMessage(UtilsTools.CODE);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}")
    public Message deleteMessage(@PathVariable List<String> id){
        sysMessageService.batchDelete(id);
        return coverMessage(UtilsTools.CODE);
    }


    /**
     * 发送消息
     */
    @ResponseBody
    @RequestMapping(value = "/sendMsg", method = RequestMethod.POST)
    public Message sendMessage(@RequestBody MsgParams msgParams) {
        Map<String, String> map = null;
        try {
            map = (Map<String, String>) JSON.parse(msgParams.getTestData());
        } catch (Exception e) {
            return coverMessage(UtilsTools.ERROR,"解析Json出错！");
        }
        //加入执行列表
        boolean isSendSuccess = pushMsgUtil.sendMessage(msgParams.getMsgType(), msgParams.getTemplateCode(), map, msgParams.getReceiver());
        if (isSendSuccess) {
            return coverMessage(UtilsTools.CODE,"发送消息任务添加成功！");
        } else {
            return coverMessage(UtilsTools.ERROR,"发送消息任务添加失败！");
        }
    }
}
